﻿Imports System
Imports System.Security.Cryptography

Public Class DecryptTransformer
    Private algorithmID As EncryptionAlgorithm

    Private initVec As Byte()


    Friend WriteOnly Property IV() As Byte()

        Set(ByVal value As Byte())
            initVec = value
        End Set
    End Property

    Friend Sub New(ByVal deCryptId As EncryptionAlgorithm)
        algorithmID = deCryptId
    End Sub

    Friend Function GetCryptoServiceProvider(ByVal bytesKey As Byte()) As ICryptoTransform
        Dim iCryptoTransform As ICryptoTransform

        Select Case algorithmID
            Case EncryptionAlgorithm.Des
                Dim dES As DES = New DESCryptoServiceProvider()
                dES.Mode = CipherMode.CBC
                dES.Key = bytesKey
                dES.IV = initVec
                iCryptoTransform = dES.CreateDecryptor()
                Exit Select

            Case EncryptionAlgorithm.TripleDes
                Dim tripleDES As TripleDES = New TripleDESCryptoServiceProvider()
                tripleDES.Mode = CipherMode.CBC
                iCryptoTransform = tripleDES.CreateDecryptor(bytesKey, initVec)
                Exit Select

            Case EncryptionAlgorithm.Rc2
                Dim rC2 As RC2 = New RC2CryptoServiceProvider()
                rC2.Mode = CipherMode.CBC
                iCryptoTransform = rC2.CreateDecryptor(bytesKey, initVec)
                Exit Select

            Case EncryptionAlgorithm.Rijndael
                Dim rijndael As Rijndael = New RijndaelManaged()
                rijndael.Mode = CipherMode.CBC
                iCryptoTransform = rijndael.CreateDecryptor(bytesKey, initVec)
                Exit Select
            Case Else

                Throw New CryptographicException([String].Concat("Algorithm ID '", algorithmID, "' not supported."))
        End Select
        Return iCryptoTransform
    End Function

End Class
